 package com.tss.controller;

import java.io.File;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tss.pojo.Apply;
import com.tss.pojo.ApplyResult;
import com.tss.pojo.Result;
import com.tss.pojo.Student;
import com.tss.service.ApplyService;
import com.tss.service.StudentService;
import com.tss.vo.Dictionary;

import net.sf.json.JSONArray;

@Controller
@RequestMapping("apply")
public class ApplyController {
	@Autowired
	private ApplyService applyService;
	
	@Autowired
	private StudentService studentService;
	
	/**
	 * 从学员档案进行录入
	 * @param apply
	 * @return
	 */
	@RequestMapping("addformstudent")
	@ResponseBody
	public String addfromstudent(Apply apply){
		Result result = new Result();
		if(apply!=null&&applyService.findApply(apply)==null){
			applyService.addApply(apply);
			result.setSuccessMessage("录入成功！");
		}else{
			result.setErrorMessage("录入失败！");
		}
		return result.toString();
	}
	
	/**
	 * 单个录入学员	报名 
	 * @param student
	 * @param courseId
	 * @return
	 */
	@RequestMapping("add")
	@ResponseBody
	public String add(Student student,Long courseId){
		Result result = new Result();
		Apply apply = new Apply();
		Student s = studentService.findStudent(student);
		if(s==null){
			result=studentService.addStudent(student);
			if(result.getStatus()==Result.STATUS_CODE_SERVER_ERRORS){
				return result.toString();
			}
			s = studentService.findStudent(student);
		}
		
		
		apply.setStudentId(s.getId());
		apply.setCourseId(courseId);
		apply.setStatus(Dictionary.APPLY_STATUS_PASSED);
		result = applyService.addApply(apply);
		return result.toString();
		
	}
	/**
	 * 学生自主报名 
	 * @param student
	 * @param courseId
	 * @return
	 */
	@RequestMapping("submit")
	@ResponseBody
	public String submit(Student student,Long courseId){
		Result result = new Result();
		Apply apply = new Apply();
		Student s = studentService.findStudent(student);
		if(s==null){
			result=studentService.addStudent(student);
			if(result.getStatus()==Result.STATUS_CODE_SERVER_ERRORS){
				return result.toString();
			}
			s = studentService.findStudent(student);
		}
		
		
		apply.setStudentId(s.getId());
		apply.setCourseId(courseId);
		apply.setStatus(Dictionary.APPLY_STATUS_PENDING);
		result = applyService.addApply(apply);
		return result.toString();
		
	}
	
	/**
	 * 删除学员
	 * @param apply
	 * @return
	 */
	@RequestMapping("delete")
	@ResponseBody
	public String delete(Apply apply){
		Result result = applyService.deleteApply(apply);
		return result.toString();
	}
	
	/**
	 * 获取审核状态的申请列表
	 * @return
	 */
	@RequestMapping("list/pending")
	@ResponseBody
	public String list(){
	//	PageHelper.startPage(1, 1);
		List<ApplyResult> list = applyService.list(Dictionary.APPLY_STATUS_PENDING);
	//	System.out.println(list.size());
	//	PageInfo<ApplyResult> p = new PageInfo<>(list);
	//	return JSONObject.fromObject(p).toString();
		return JSONArray.fromObject(list).toString();
	}
	
	
	
	
	
	@RequestMapping("pass")
	@ResponseBody
	public String pass(Long id){
		Result result = new Result();
		Apply apply = new Apply();
		apply.setId(id);
		apply=applyService.findApply(apply);
		if(apply==null){
			result.setErrorMessage("审核失败！");
			return result.toString();
		}
		apply.setStatus(Dictionary.APPLY_STATUS_PASSED);
		result = applyService.updateApply(apply);
		return result.toString();
	}
	
	@RequestMapping("reject")
	@ResponseBody
	public String reject(Long id){
		Result result = new Result();
		Apply apply = new Apply();
		apply.setId(id);
		apply=applyService.findApply(apply);
		if(apply==null){
			result.setErrorMessage("审核失败！");
			return result.toString();
		}
		apply.setStatus(Dictionary.APPLY_STATUS_REFUSED);
		result = applyService.updateApply(apply);
		return result.toString();
	}
	
	
	
	@RequestMapping("notice")
	@ResponseBody
	public String notice(){
		//undo
		//发送通知
		return null;
	}
	
	@RequestMapping("upload")
	@ResponseBody
	public String upload(File file){
		Result result = new Result();
		return applyService.importApply(file).toString();
	}
	

	
	
	
}
